var CheckoutMethod = {//收费方式管理初始化
    id: "CheckoutMethodTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

CheckoutMethod.initColumn = function () {//初始化表格的列
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '编码', field: 'num', align: 'center', valign: 'middle', sortable: true},
        {title: '名称', field: 'checkoutMethod', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
        {
            title: '收费账单', field: '', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                var html = '';
                if (row.chargeBillStatus == '0') {
                    html += '<div style="display: flex;align-items: center;"><span id="charge_bill_status" onclick="CheckoutMethod.changeTypeStatus(\'' + row.id + '\',\'charge_bill_status\',this)" style="flex: 1;" class="switch_on"></span><span style="flex: 2.5;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD">';
                }
                if (row.chargeBillStatus == '1') {
                    html += '<div style="display: flex;align-items: center;"><span id="charge_bill_status" onclick="CheckoutMethod.changeTypeStatus(\'' + row.id + '\',\'charge_bill_status\',this)" style="flex: 1;" class="switch_on switch_off"></span><span style="flex: 2.5;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD">';
                }
                if (row.chargeBillIsInCash == '0') {
                    html += '<label style="display: block"><input id="charge_bill_is_inCash" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_inCash\',this)"  type="checkbox" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入现金流</span></label>'
                }
                if (row.chargeBillIsInCash == '1') {
                    html += '<label style="display: block"><input id="charge_bill_is_inCash" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_inCash\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入现金流</span></label>'
                }
                if (row.chargeBillIsBusinessIncome == '0') {
                    html += '<label style="display: block"><input id="charge_bill_is_business_income" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_business_income\',this)"  type="checkbox" name="chargeBillIsInCash" value="营业收入"><span style="margin-left: 10px;">计入营业收入</span></label>'
                }
                if (row.chargeBillIsBusinessIncome == '1') {
                    html += '<label style="display: block"><input id="charge_bill_is_business_income" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_business_income\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="营业收入"><span style="margin-left: 10px;">计入营业收入</span></label>'
                }
                if (row.chargeBillIsAchievements == '0') {
                    html += '<label style="display: block"><input id="charge_bill_is_achievements" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_achievements\',this)"  type="checkbox" name="chargeBillIsInCash" value="绩效"><span style="margin-left: 10px;">计入绩效</span></label>'
                }
                if (row.chargeBillIsAchievements == '1') {
                    html += '<label style="display: block"><input id="charge_bill_is_achievements" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_achievements\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="绩效"><span style="margin-left: 10px;">计入绩效</span></label>'
                }
                if (row.chargeBillIsDocAchievement == '0') {
                    html += '<label style="display: block"><input id="charge_bill_is_docAchievement" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_docAchievement\',this)"  type="checkbox" name="chargeBillIsInCash" value="医生业绩"><span style="margin-left: 10px;">计入医生业绩</span></label>'
                }
                if (row.chargeBillIsDocAchievement == '1') {
                    html += '<label style="display: block"><input id="charge_bill_is_docAchievement" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'charge_bill_is_docAchievement\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="医生业绩"><span style="margin-left: 10px;">计入医生业绩</span></label>'
                }
                return html;
            }
        },
        /*        {title: '储值卡充值', field: '', align: 'center', valign: 'middle', sortable: true,
                    formatter: function (value, row, index) {
                        return "<div style='display: flex;align-items: center;'><span style='flex: 1' class='switch_on'></span><span style='flex: 3;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD'>" +
                            "<label style='display: block'><input type='checkbox' name='cashFlow' value='现金流'><span style='margin-left: 10px;'>计入现金流</span></label>" +
                            "<label style='display: block'><input type='checkbox' name='businessIncome' value='营业收入'><span style='margin-left: 10px;'>计入营业收入</span></label></span></div>";
                    }
                },*/
        {
            title: '卡券发售', field: '', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                var html = '';
                if (row.cardTicketStatus == '0') {
                    html += '<div style="display: flex;align-items: center;"><span id="card_ticket_status" onclick="CheckoutMethod.changeTypeStatus(\'' + row.id + '\',\'card_ticket_status\',this)" style="flex: 1;" class="switch_on"></span><span style="flex: 2.5;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD">';
                }
                if (row.cardTicketStatus == '1') {
                    html += '<div style="display: flex;align-items: center;"><span id="card_ticket_status" onclick="CheckoutMethod.changeTypeStatus(\'' + row.id + '\',\'card_ticket_status\',this)" style="flex: 1;" class="switch_on switch_off"></span><span style="flex: 2.5;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD">';
                }
                if (row.cardTicketIsInCash == '0') {
                    html += '<label style="display: block"><input id="card_ticket_is_inCash" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'card_ticket_is_inCash\',this)"  type="checkbox" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入现金流</span></label>'
                }
                if (row.cardTicketIsInCash == '1') {
                    html += '<label style="display: block"><input id="card_ticket_is_inCash" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'card_ticket_is_inCash\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入现金流</span></label>'
                }
                if (row.cardTicketIsBusinessIncome == '0') {
                    html += '<label style="display: block"><input id="card_ticket_is_business_income" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'card_ticket_is_business_income\',this)"  type="checkbox" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入营业收入</span></label>'
                }
                if (row.cardTicketIsBusinessIncome == '1') {
                    html += '<label style="display: block"><input id="card_ticket_is_business_income" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'card_ticket_is_business_income\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入营业收入</span></label>'
                }
                if (row.cardTicketIsDocAchievement == '0') {
                    html += '<label style="display: block"><input id="card_ticket_is_docAchievement" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'card_ticket_is_docAchievement\',this)"  type="checkbox" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入医生业绩</span></label>'
                }
                if (row.cardTicketIsDocAchievement == '1') {
                    html += '<label style="display: block"><input id="card_ticket_is_docAchievement" onclick="CheckoutMethod.changePaymentMethod(\'' + row.id + '\',\'card_ticket_is_docAchievement\',this)"  type="checkbox" checked="checked" name="chargeBillIsInCash" value="现金流"><span style="margin-left: 10px;">计入医生业绩</span></label>'
                }
                return html;
            }
        }
    ];
};

CheckoutMethod.changeTypeStatus = function (id, column, obj) {
    var status = "";
    if ($(obj).hasClass('switch_off')) {
        $(obj).removeClass('switch_off');
        status = "0";
    } else {
        $(obj).toggleClass('switch_off');
        status = "1";
    }
    if (confirm("是否确认修改")) {
        Feng.success('操作成功!');
        var ajax = new $ax(Feng.ctxPath + "/checkoutMethod/updateCheckoutMethodStatus", function (data) {
            Feng.success("变更成功!");
        }, function (data) {
            Feng.error("变更失败!");
        });
        ajax.set("id", id);
        ajax.set("column", column);
        ajax.set("status", status);
        ajax.start();
    } else {
        if (status == "0") {
            $(obj).toggleClass('switch_off');
        } else {
            $(obj).removeClass('switch_off');
        }
    }
};

CheckoutMethod.changePaymentMethod = function (id, column, obj) {//修改支付状态
    var status = "";
    if (obj.checked) {
        status = "1";
    } else {
        status = "0";
    }
    if (confirm("是否确认修改")) {
        Feng.success('操作成功!');
        var ajax = new $ax(Feng.ctxPath + "/checkoutMethod/updateCheckoutMethodStatus", function (data) {
            Feng.success("变更成功!");
        }, function (data) {
            Feng.error("变更失败!");
        });
        ajax.set("id", id);
        ajax.set("column", column);
        ajax.set("status", status);
        ajax.start();
    } else {
        if (status == "1") {
            obj.checked = false;
        } else {
            obj.checked = true;
        }
    }
};

CheckoutMethod.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CheckoutMethod.seItem = selected[0];
        return true;
    }
};

CheckoutMethod.openAddCheckoutMethod = function () {//点击添加收费方式
    var index = layer.open({
        type: 2,
        title: '添加收费方式',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/checkoutMethod/checkoutMethod_add'
    });
    this.layerIndex = index;
};

CheckoutMethod.openCheckoutMethodDetail = function () {//打开查看收费方式详情
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '收费方式详情',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/checkoutMethod/checkoutMethod_update/' + CheckoutMethod.seItem.id
        });
        this.layerIndex = index;
    }
};

CheckoutMethod.delete = function () {//删除收费方式
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/checkoutMethod/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            CheckoutMethod.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};

CheckoutMethod.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['checkoutMethod'] = $("#checkoutMethod").val();
    return queryData;
};

CheckoutMethod.resetSearch = function () {$("#checkoutMethod").val("");CheckoutMethod.search();};

CheckoutMethod.search = function () {//查询列表
    CheckoutMethod.table.refresh({query: CheckoutMethod.formParams()});
};

$(function () {
    var defaultColunms = CheckoutMethod.initColumn();
    var table = new BSTable(CheckoutMethod.id, "/checkoutMethod/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CheckoutMethod.formParams());
    CheckoutMethod.table = table.init();
    CheckoutMethod.table = table;
    var configureTitle = $("#configureTitle > span");
    var configureContent = $("#configureContent>div");
    configureContent.hide();
    configureTitle.eq(0).css({"border-bottom": "3px solid #2eadd0", "color": "#2eadd0"});
    configureContent.eq(0).show();
    configureTitle.click(function () {
        $(this).css({"border-bottom": "3px solid #2eadd0", "color": "#2eadd0"}).siblings().css({"border": "none","color": "inherit"});
        configureContent.eq(configureTitle.index($(this))).show().siblings().hide();
    });
    $("#deductionShow").click(function () {$("#deductionContent").children('div').eq(1).nextAll().remove();$("#deductionBox").show();});
    $("#cancel").click(function () {$("#deductionContent").children('div').eq(1).nextAll().remove();$("#deductionBox").hide();});
    var step = 1;
    $("#deductionContent").on('click', '.deductionAdd', function () {
        if ($(this).hasClass('glyphicon-plus')) {
            step++;var Box = $(this).parent().parent().clone();
            Box.find('.deductionAdd').attr('class', 'glyphicon glyphicon-minus deductionAdd');
            Box.find('.step').text(step);$(this).parents('#deductionContent').append(Box)
        } else {$(this).parents('#deductionContent').children('div:last').remove();step--;}
    })
});